package top.abrsm.controller;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import top.abrsm.entity.CommentEntity;
import top.abrsm.entity.FileEntity;
import top.abrsm.entity.ProductEntity;
import top.abrsm.service.ICommentService;
import top.abrsm.service.IFileService;
import top.abrsm.service.IProductService;
import top.abrsm.util.ConstUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2023-11-18
 */
@Controller
public class ProductController {

    @Resource
    private IProductService iProductService;
    @Resource
    private IFileService iFileService;
    @Resource
    private ICommentService iCommentService;

    /**
     * 查询所有商品列表
     *
     * @param productEntity
     * @return
     */
    @RequestMapping ( {"/product/queryList", "/"} )
    public String queryList ( ProductEntity productEntity, Model model ) {
        List<ProductEntity> productEntities = iProductService.queryList ( productEntity );
        for ( ProductEntity product : productEntities ) {
            FileEntity fileEntity = new FileEntity ();
            fileEntity.setFileJoinType ( ConstUtil.FILE_PRODUCT );
            fileEntity.setFileJoinId ( product.getId () );
            List<FileEntity> fileEntities = iFileService.queryList ( fileEntity );
            product.setFileEntities ( fileEntities );
        }
        model.addAttribute ( "productEntities", productEntities );
        return "productList";
    }

    @RequestMapping ( "/product/querySingle" )
    public String querySingle ( @Param ( "id" ) String id, Model model ) {
        ProductEntity productEntity = new ProductEntity ();
        productEntity.setId ( id );
        productEntity = iProductService.querySingle ( productEntity );
        FileEntity fileEntity = new FileEntity ();
        fileEntity.setFileJoinType ( ConstUtil.FILE_PRODUCT );
        fileEntity.setFileJoinId ( id );
        List<FileEntity> fileEntities = iFileService.queryList ( fileEntity );
        productEntity.setFileEntities ( fileEntities );
        model.addAttribute ( "productEntity", productEntity );
        CommentEntity commentEntity = new CommentEntity ();
        List<CommentEntity> commentEntities = iCommentService.queryList ( commentEntity );
        Integer count = iCommentService.queryCount ( commentEntity );
        model.addAttribute ( "commentEntities", commentEntities );
        model.addAttribute ( "commentCount", count );
        return "productInfo";
    }

}
